package nl.boektrust.btsoep.client;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class EditBookVerticalPanelSimpleTextbox extends EditBookVerticalPanel implements BlurHandler,  ValueChangeHandler<String> {
	TextBox theTextBox = new TextBox();
	EditBookDialog parentPanel;
	//String value;
	
	public EditBookVerticalPanelSimpleTextbox(String label, EditBookDialog parentPanel) {
		super(label);		
		theTextBox.addValueChangeHandler(this);
		theTextBox.addBlurHandler(this);
		theTextBox.setFocus(true);
		editPanel.add(theTextBox);
		this.parentPanel = parentPanel;
		setFieldLabel(label);
		setToDisplayMode();
	}
	
	public EditBookVerticalPanelSimpleTextbox(String label) {
		this(label, null);
	}

	public EditBookVerticalPanelSimpleTextbox() {
		this("");
	}
	
	void setTextBoxValue(Object value) {
		if (value != null && ((String)value).length() > 0) theTextBox.setText((String)value);
	}

	@Override void setToEditMode() {
		super.setToEditMode();
		theTextBox.setFocus(true);
		if (value != null && ((String)value).length() > 0) theTextBox.setText((String)value); 

	}
	
	@Override void setToDisplayMode() {
		super.setToDisplayMode();
		if (value != null && ((String)value).length() > 0) theLabel.setText((String)value); 
	}
	/**
	 * @return the value
	 */
	public String getValue() {
		return (String)value;
	}

	/**
	 * @param value the value to set. If null or empty, the placeholder text will be shown. 
	 */
	public void setValue(String value) {
		super.value = value;
		
		//if (super.getLabel().getText().equals("ISBN13")) System.out.println("textbox:setValue:ISBN13:"+super.getLabel());
		if (theLabel == null) System.out.println("theLabel is null"); else {
			//if (super.getLabel().getText().equals("ISBN13")) System.out.println("textbox:setValue:ISBN13 theLabel is not NULL, its value is: "+theLabel);
		}
		if (value != null && value.length() > 0)	{
			//if (super.getLabel().getText().equals("ISBN13")) System.out.println("textbox:setValue:ISBN13 value.length is not 0, its value is: "+value.length());
			theLabel.setText(value);
		} else {
			theLabel.setText("nog niet ingevuld");
		}
		setTextBoxValue(value);


	}

	public void onValueChange(ValueChangeEvent<String> event) {
		setValue(theTextBox.getValue());
		if (parentPanel != null) parentPanel.panelHasChanged();
		setToDisplayMode();
		valueHasChanged(theTextBox.getValue());
	}
	
	public void valueHasChanged(String newValue) {
		
	}


	// if the edit element loses focus, set to display mode. 
	public void onBlur(BlurEvent event) {
		setToDisplayMode();
	}

}
